package com.sec.etech.file.model;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.sec.etech.file.vo.EtechFileVersionVo;
import org.openbpm.sys.core.model.SysFile;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@lombok.Data
@TableName("sys_file")
public class EtechFile extends SysFile {

    @TableId("id_")
    private String id;
    @TableField(value = "create_time_", fill = FieldFill.INSERT)
    private Date createTime;
    @TableField(value = "create_by_",fill = FieldFill.INSERT)
    private String createBy;
    @TableField(value = "update_time_", fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;
    @TableField(value = "update_by_", fill = FieldFill.INSERT_UPDATE)
    private String updateBy;

    @TableField("year_")
    private int year; //所属年度, 冗余，如数据量太多可作为分区字段

    @TableField("name_")
    private String name;
    //模块路径
    @TableField("module_")
    private String module;

    @TableField("uploader_")
    private String uploader;

    @TableField("path_")
    private String path;

    @TableField("rights_json_")
    private String rightsJson;
    // r-读取/浏览，w-写入/修改
//    {
//        "r": [
//        {
//            "id": "417249932832407553,417249941086535681",
//                "type": "role",
//                "desc": "（角色）[销售经理,技术人员]"
//        }
//          ],
//        "w": [
//        {
//            "id": "417249941086535681",
//                "type": "role",
//                "desc": "（角色）[技术人员]"
//        }
//          ]
//    }
    @TableField(exist = false)
    private Map<String, JSONArray> rights = new HashMap();

    @JsonProperty("当前用户的文件预览权限")
    @TableField(exist = false)
    private boolean hasPreviewRight4currentUser = true;
    @JsonProperty("当前用户的预览文件下载权限")
    @TableField(exist = false)
    private boolean hasPreviewDRight4currentUser = true;
    @JsonProperty("当前用户的原始文件下载权限")
    @TableField(exist = false)
    private boolean hasDownloadRight4currentUser = true;

    /*private void checkRights() {
        checkRights(null);
        // 增加当前用户的权限设置
        JSONArray readRight = rights.get("r");
        if (readRight != null) {
            hasPreviewRight4currentUser = PermissionCalculatorFactory.haveRights(readRight);
        }
        if (hasPreviewRight4currentUser) {
            JSONArray downloadRight = rights.get("d");
            if (downloadRight != null) {
                hasDownloadRight4currentUser = PermissionCalculatorFactory.haveRights(downloadRight);
            }
            JSONArray previewDownloadRight = rights.get("pd");
            if(previewDownloadRight!=null) {
                hasPreviewDRight4currentUser = PermissionCalculatorFactory.haveRights(previewDownloadRight);
            }
        } else {
            hasDownloadRight4currentUser = false;
            hasPreviewDRight4currentUser = false;
        }
    }*/

    /*private void checkRights(String rightsJson) {
        com.alibaba.fastjson.JSONObject jsonObject = null;
        if (StringUtil.isNotEmpty(rightsJson)) {
            jsonObject = com.alibaba.fastjson.JSONObject.parseObject(rightsJson);
            rights.clear();
        }
        if (rights.isEmpty()) {
            if (jsonObject == null) {
                jsonObject = com.alibaba.fastjson.JSONObject.parseObject(rightsJson);
            }
            if (jsonObject != null) {
                for (String key : jsonObject.keySet()) {
                    rights.put(key, new JSONArray(jsonObject.getJSONArray(key)));
                }
            }
        }
    }*/

    /*public JSONArray getReadRights() {
        checkRights();
        return rights.get(RightsType.READ.getKey());
    }*/

    /*public boolean havePreviewRights() {

        if (ContextUtil.currentUserIsAdmin()) {
            return true;
        }

        JSONArray readRight = getReadRights();
        if (readRight == null) {
            return true;
        }
        // 调用系统权限判断
        return PermissionCalculatorFactory.haveRights(readRight);
    }*/

    /*public void setReadRight(JSONArray rightsValue) {
        rights.put(RightsType.READ.getKey(), rightsValue);
        this.rightsJson = JSONUtil.toJsonPrettyStr(JSONUtil.parseObj(rights));
    }

    @Override
    public void setRightsJson(String rightsJson) {
        this.rightsJson = rightsJson;
        checkRights(rightsJson);
    }*/

    @Version
    @TableField("version_")
    protected int version = 0;
    @TableLogic
    @TableField("delete_")
    protected boolean delete = false;
    @TableField("file_version_")
    protected String fileVersion;
    //最新版本标志：1-是、0-否
    @TableField("latest_")
    private int latest;
    @TableField("md5")
    private String md5;

    //path, 文件path（如为相对路径，则相对上传根目录）
    //previewPath，预览文件路径，如为相对路径，则相对预览根目录
    @TableField("preview_")
    private String previewPath;
    //缩略图文件
    @TableField("thumbnail_")
    private String thumbnailPath;

    //文件唯一标识
    @TableField("unique_identifier_")
    private String uniqueIdentifier;
    /////// 以下字段仅用于查看C#系统迁移过来的数据，基于uploadseries获取。

    //type: xlsx,pdf
    //mime: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/pdf
    //length
    @TableField("file_type_")
    private String fileType;
    @TableField("file_mime_")
    private String fileMime;
    @TableField("file_length")
    private Long fileLength;

    //platJson(plat_key, plat_page, plat_value)
    @TableField("plat_key")
    private String platKey;
    @TableField("plat_page_")
    private String platPage;
    @TableField("plat_value_")
    private String platValue;

    // 统计字段
    @TableField("count_online_")
    private Long online;
    @TableField("count_download_")
    private Long download;

    //lastCountTime 最后统计时间
    @TableField("last_count_time")
    private Date lastCountTime;

    @TableField(exist = false)
    protected String createByName;

    //日志记录
    @TableField(exist = false)
    protected List<EtechFileLog> logList;

    //文件的版本列表
    @TableField(exist = false)
    protected List<EtechFileVersionVo> versionList;

    //预览文件url
    @TableField(exist = false)
    private String previewUrl;

    /**
     * 是否新的webuploader上传器
     *
     * @return
     */
    public boolean isWebUploader() {
        return "etech-web-uploader".equalsIgnoreCase(this.uploader);
    }

}
